<?php
// $Id: shop.php 375 2012-05-22 07:21:29Z tomqin@gmail.com $

class Shop_Controller extends FrontController{

    var $res_id;
    var $menu_type;

    function init(){
        $this->res_id = intval($this->action);

        if($this->res_id == 1003){
            DB::update('t_store', array('views' => '+=1'), "res_id = '{$this->res_id}'");
            header("Location: http://lihua.sudingke.com");

            exit;
        }

        $this->zone = Base::loadModule('zone', true);
        $this->zone->initCity();
    }

    function _onDefault(){
        $store  = $this->getStore();
        if($store){
            //$dishcat = $this->getDishCat();
            $dishes  = $this->getDishes($dishcat);

            $store['url'] = Base::frontUrl('shop', $store['res_id']);
            $this->setVar('encryptId', Base::crypt($store['res_id']));
            $this->setVar('store', $store);
            $this->setVar('dishcat', $dishcat);
            $this->setVar('dishes', $dishes);

            //更新数据 reads
            DB::update('t_store', array('views' => '+=1'), "res_id = '$store[res_id]'");
        }

        $bigzone  = $this->zone->getZoneById($store['zone_id'])->name;
        $subzone  = $this->zone->getZoneById($store['subzone_id'])->name;
        $seo_name = $store['name'];
        $seo_zone = $bigzone.$subzone;

        $title   = "{$seo_name}外卖网_外卖电话_外卖信息_{$seo_zone}快餐外卖送餐_网上订餐";

        $this->seoTitle($title);
        $this->setVar('bigzone', $bigzone);
        $this->setVar('subzone', $subzone);
        $this->setVar('mode', $this->param ? $this->param : 'list');

        $this->render('shop-index');
    }

    private function getStore(){
        return DB::getOne(DB::getSql('*', 't_store', array('res_id' => $this->res_id)));
    }

//  private function getDishCat(){
//      return DB::getAll(DB::getSql('*', 't_dish_cat', array('res_id' => $this->res_id), null, 'ordnum ASC'), null, 'dish_cat_id');
//  }

    private function getDishes(&$dishcats){
        $dishcats = DB::getAll(DB::getSql('*', 't_dish_cat', array('res_id' => $this->res_id), null, 'ordnum ASC'), null, 'dish_cat_id');
        $dishes   = DB::getAll(DB::getSql('*', 't_dish', array('res_id' => $this->res_id), null, 'dish_id ASC'));
        foreach($dishes as $dish){
            $cat_id = $dish['dish_cat_id'];
            $cat_id = isset($dishcats[$cat_id]) ? $cat_id : 0;

            $data[$cat_id][$dish['dish_id']] = $dish;
        }
        if(isset($data[0])){
            $tmp = $data[0];
            unset($data[0]);
            $data[0] = $tmp;
        }

        return $data;
    }
}
?>